import router from './index'
import { useUserStore } from '@/stores/user'

import NProgress from 'nprogress' // 导入 nprogress模块

import 'nprogress/nprogress.css' // 导入样式，否则看不到效果

NProgress.configure({ showSpinner: false }) // 显示右上角螺旋加载提示

router.beforeEach((to, from, next) => {
  // 设置网页标题
  document.title = '硅谷优选 -' + to.meta.title
  NProgress.start()
  const userStore = useUserStore()
  if (userStore.token) {
    if (to.path === '/login') {
      return next('/')
    }
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
